﻿@model List<string>
@{
    ViewBag.Title = "Email";
}

<div id="Wrapper2">
    <div id="content_header">
        <div id="headerimage">
        </div>
        <div id="theheader">@ViewBag.Header</div> 
    </div>
    <!-- Left Column begins here -->
    <aside id="sidebar-wrapper">
        <nav class="sidebar">
            <h1>Compose Email</h1>
            <!-- CONTENT GOES HERE -->
            @using (Html.BeginForm("Compose", "Email", FormMethod.Post, new { id = "emailform" }))
            {
                thailandProject.Models.CLIENT sessionClient = (thailandProject.Models.CLIENT)Session["ContextClient"];
                string emailAddress = "";
                string clientName = "";
                string message = Session["emailMessage"].ToString();
                string clientID = "";
                if (sessionClient != null)
                {
                    emailAddress = sessionClient.clientEmail;
                    clientName = (String.IsNullOrEmpty(sessionClient.clientLastName) ? sessionClient.clientFirstName : sessionClient.clientFirstName + " " + sessionClient.clientLastName);
                    message = Session["emailMessage"].ToString();
                    message = message.Replace("<<Insert Name>>", clientName);
                    clientID = sessionClient.clientID + "";
                }
                
                <div class="w100 floatleft">
                <div class="floatleft" id="emailComposeToDiv">Email To <br /><input id="to" name="to" type="text" onchange="emailsearch(this)" value="@emailAddress" /></div>
                <div class="floatleft" id="emailComposeNameBoxes"><span id="lblEmailComposeFirstName">Name</span> <br />
                    <div id="clientFirstNameBox"><input name="clientFirstName" id="clientFirstName" type="text" onkeyup="nameset()" value="@clientName" /></div>
                    
                </div>
                <div class="floatleft" id="emailComposeSubjectDiv">Subject <br /><input id="subject" name="subject" type="text" value="Response to your Inquiry" /></div>
                <div class="floatleft w100">Body <br /><textarea name="body" id="body" style="width:95%; height:200px; border:1px solid #999999;">@message</textarea><span id="emailEasterEgg" style="visibility:hidden;">So... um... did you know the max length for this field is 524,000 characters?</span></div>
                <input type="hidden" name="clientID" id="clientID" value="@clientID" />
                <input type="hidden" name="donotsend" id="donotsend" value="false" />
                <span class="floatleft w60" style="margin-top:10px; margin-right:10px;"><img id="sendemail" src="../../images/sendemailicon.png" />
                    <img id="donotsendemail" src="../../images/button_do_not_send.png" /></span>
            </div>
            <div id="emailError" style="color:red; visibility:hidden">Please provide a valid email, a client name, and a subject.</div>
            }
        </nav>
        
        <!-- Left column ends here -->
    </aside>
    <!-- Right column begins here -->
    <article id="contents">
        Enter the client's email address into the "Email To" field. If the client is a return client, the information
        will automatically fill in.
        <br /><br />
        If the client is new, enter the client's email and name. The system will automatically create a new client on
        submission.
    </article>
    <!-- Right column ends here -->
</div>
<!-- Div wrapper2 ends here -->

<script>
    $("document").ready(function () {
        $().ready(function () {
            //Give focus to "To" box
            $("#to").focus();
        })

        //alert("ready1");
        $("#sendemail").click(function () {
            //alert("worked");
            // alert($("input").each().val());
            //Check to make sure a valid email address was entered, a name was entered, and a subject is entered
            var email = $("#to").val();
            var clientName = $("#clientFirstName").val();
            var subject = $("#subject").val();

            if (email.length > 0 && clientName.length > 0 && subject.length > 0) {
                $("#emailError").attr('style', 'color:red; visibility:hidden');
                $("#emailform").submit();
            }
            else {
                $("#emailError").attr('style', 'color:red; visibility:visible');
            }
            
            //alert("end");
        })

        $("#donotsendemail").click(function () {
            //alert("worked");
            // alert($("input").each().val());
            //Check to make sure a valid email address was entered, a name was entered, and a subject is entered
            var email = $("#to").val();
            var clientName = $("#clientFirstName").val();
            var subject = $("#subject").val();

            if (email.length > 0 && clientName.length > 0 && subject.length > 0) {
                $("#emailError").attr('style', 'color:red; visibility:hidden');

                if (confirm('Are you sure?\n\nThe reservation will still be made, but the email will not be sent.')) {
                    $("#donotsend").val('true');
                    $("#emailform").submit();
                }
                
            }
            else {
                $("#emailError").attr('style', 'color:red; visibility:visible');
            }

            //alert("end");
        })

        $("#body").keyup(function() {
            //The maximum length is set at 524,000 characters
            var message = $("#body").val();

            if (message.length > 524000) {
                message = message.substr(0, 524000);
                $("#body").val(message);
                $("#emailEasterEgg").attr('style', 'visibility:visible;');
            }
        })
    })
</script>

<script>
    function emailsearch(object)
    {
        $.ajax({
            url: "/email/EmailQuickSearch", type: "POST", dataType: "json",

            data: "email=" + object.value,

            success: function (name)
            {
                if (name == null)
                {
                    //$("#clientFirstName").val("");
                    //$("#clientLastName").val("");
                    $("#clientID").val("-1");
                    nameset();
                    return;
                }

                var id = name.substr(0, name.indexOf("-"));
                name = name.substr(name.indexOf("-") + 1, name.length - name.indexOf("-") - 1);

                var firstName = name.indexOf(" ") != -1 ? name.substr(0, name.indexOf(" ")) : name;
                var lastName = name.indexOf(" ") != -1 ? name.substr(name.indexOf(" ") + 1, name.length - 1) : "";

                $("#clientID").val(id);
                $("#clientFirstName").val(firstName + (lastName == "" ? "" : " " + lastName));
                //$("#clientLastName").val(lastName);

                nameset();

                //document.getElementById("clientFirstName").value = firstName;
                //document.getElementById("clientLastName").value = lastName;
            }
        });
    }

    function nameset()
    {
        var firstName = $("#clientFirstName").val();

        var message = $("#body").val();

        message = "Dear " + firstName + "," +
            message.substr(message.indexOf(",") + 1, message.length - message.indexOf(","));

        $("#body").val(message);
    }
</script>